// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var AudioManager = require('AudioManager')
var ClickManager = require('ClickManager')

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        soundBtnNode: cc.Node,
        musicBtnNode: cc.Node,

        exitBtnNode: cc.Node,

        soundSlider: cc.Slider,
        musicSlider: cc.Slider,

        soundProgressBar: cc.ProgressBar,
        musicProgressBar: cc.ProgressBar,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 设置默认音效
        this.initOrUpdateVolumes()

        // 音乐
        this.musicSlider.node.on('slide', (slider) => {
            this.musicProgressBar.progress = slider.progress
            AudioManager.setVolume(1, slider.progress)
        }, this)

        // 音效
        this.soundSlider.node.on('slide', (slider) => {
            this.soundProgressBar.progress = slider.progress
            AudioManager.setVolume(2, slider.progress)
        }, this)

        // 最大
        ClickManager.onClick(this.musicBtnNode, this, () => {
            AudioManager.setVolume(1, 1)
            this.initOrUpdateVolumes()
        })
        ClickManager.onClick(this.soundBtnNode, this, () => {
            AudioManager.setVolume(2, 1)
            this.initOrUpdateVolumes()
        })

        // 切换账号
        ClickManager.onClick(this.exitBtnNode, this, () => {
            ClickManager.exitLogin()
        })
    },

    start () {

    },

    // 初始化或者更新音量
    initOrUpdateVolumes () {
        let volumes = AudioManager.getVolume()
        this.setProgressVolumes(volumes.sound, volumes.music)
    },

    // 设置音量
    setProgressVolumes (suond, music) {
        this.soundSlider.progress = suond
        this.musicSlider.progress = music
        this.soundProgressBar.progress = suond
        this.musicProgressBar.progress = music
    }

    // update (dt) {},
});
